package com.mldong.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mldong.holder.LoginUserHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Date now = new Date();
        Long userId = LoginUserHolder.getUserId();
        this.fillStrategy(metaObject,"createTime",now);
        this.fillStrategy(metaObject,"createUser",userId);
        this.fillStrategy(metaObject,"updateTime",now);
        this.fillStrategy(metaObject,"updateUser",userId);
        this.fillStrategy(metaObject,"isDeleted",false);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date now = new Date();
        Long userId = LoginUserHolder.getUserId();
        this.fillStrategy(metaObject,"updateTime",now);
        this.fillStrategy(metaObject,"updateUser",userId);
    }
}
